#include <stdint.h>
#include "gui_base.h"

void gui_base_rectangle(uint16_t x0, uint16_t y0, uint16_t width, uint16_t high)
{
	Lcd_DrawVLine(x0, y0, high);
	Lcd_DrawHLine(x0, y0, width);
	Lcd_DrawVLine(x0+width-1, y0, high);
	Lcd_DrawHLine(x0, y0+high-1, width);
}

void gui_base_fillrect(uint16_t x0, uint16_t y0, uint16_t width, uint16_t high)
{
	uint16_t i;
	
	for(i=0; i<high; i++)
	{
		Lcd_DrawHLine(x0, y0+i, width);
	}
}

void gui_base_invertrect(uint16_t x0, uint16_t y0, uint16_t width, uint16_t high)
{
	uint16_t i;
	
	for(i=0; i<high; i++)
	{
		Lcd_DrawInvertHLine(x0, y0+i, width);
	}
}

void gui_base_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
	int i, deltax, deltay, numpixels, d, dinc1, dinc2, x, xinc1, xinc2, y, yinc1, yinc2;
    
  if(x1-x0 < 0)
	{
		deltax = x0-x1;
	}
    else
	{
		deltax = x1-x0;
	}
    if(y1-y0 < 0)
	{
		deltay = y0-y1;
	}
    else
	{
		deltay = y1-y0;
	}
	
	if(deltax >= deltay)
	{
		numpixels = deltax + 1;
		d = (2 * deltay) - deltax;
		dinc1 = deltay << 1;
		dinc2 = (deltay - deltax) << 1;
		xinc1 = 1;
		xinc2 = 1;
		yinc1 = 0;
		yinc2 = 1;
	}
	else
	{
		numpixels = deltay + 1;
		d = (2 * deltax) - deltay;
		dinc1 = deltax << 1;
		dinc2 = (deltax - deltay) << 1;
		xinc1 = 0;
		xinc2 = 1;
		yinc1 = 1;
		yinc2 = 1;
	}
	
	if(x0 > x1)
	{
		xinc1 = - xinc1;
		xinc2 = - xinc2;
	}
	if(y0 > y1)
	{
		yinc1 = - yinc1;
		yinc2 = - yinc2;
	}
	
	x = x0;
	y = y0;
	
	for(i= 1; i< numpixels; i++)
	{
		Lcd_DrawPixel(x, y, 1, 1);
		if (d < 0)
		{
			d = d + dinc1;
			x = x + xinc1;
			y = y + yinc1;
		}
		else
		{
			d = d + dinc2;
			x = x + xinc2;
			y = y + yinc2;
		}
	}
}

void gui_base_circle(uint16_t x, uint16_t y, uint16_t radius)
{
	int32_t   d;
	uint32_t  cur_x;
	uint32_t  cur_y;
	
	d = 3 - (radius << 1);
	cur_x = 0;
	cur_y = radius;
	
	while (cur_x <= cur_y)
	{
		Lcd_DrawPixel(x + cur_x, y + cur_y, 1, 1);
		Lcd_DrawPixel(x + cur_x, y - cur_y, 1, 1);
		Lcd_DrawPixel(x - cur_x, y + cur_y, 1, 1);
		Lcd_DrawPixel(x - cur_x, y - cur_y, 1, 1);
		Lcd_DrawPixel(x + cur_y, y + cur_x, 1, 1);
		Lcd_DrawPixel(x + cur_y, y - cur_x, 1, 1);
		Lcd_DrawPixel(x - cur_y, y + cur_x, 1, 1);
		Lcd_DrawPixel(x - cur_y, y - cur_x, 1, 1);
		
		if (d < 0)
		{
			d += (cur_x << 2) + 6;
		}
		else
		{
			d += ((cur_x - cur_y) << 2) + 10;
			cur_y--;
		}
		cur_x++;
	}
}

void gui_base_msgbox(uint16_t width, uint16_t high)
{
	uint16_t x, y;
	
	x = (192-width)/2;
	y = (64-high)/2;
	
	gui_base_rectangle(x, y, width, high);
	
	Lcd_DrawHLine(x+2, y+high, width);
	Lcd_DrawHLine(x+2, y+high+1, width);
	Lcd_DrawVLine(x+width,  y+2, high);
	Lcd_DrawVLine(x+width+1, y+2, high);
}

void gui_base_rim(uint16_t x, uint16_t y, uint16_t width, uint16_t high, uint16_t w, uint16_t h)
{
	gui_base_line(x, y, x, y+h);
	gui_base_line(x, y, x+w, y);
	
	gui_base_line(x, y+high, x+w, y+high);
	gui_base_line(x, y+high, x, y+high-h);
	
	gui_base_line(x+width, y, x+width-w, y);
	gui_base_line(x+width, y, x+width, y+h);
	
	gui_base_line(x+width, y+high, x+width-w, y+high);
	gui_base_line(x+width, y+high, x+width, y+high-h);
}

